/*
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

For example:
Given binary tree {3,9,20,#,#,15,7},
    3
   / \
  9  20
    /  \
   15   7
return its level order traversal as:
[
  [3],
  [9,20],
  [15,7]
]
*/

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int> > levelOrder(TreeNode *root) {
        vector<vector<int> > res;
        queue<TreeNode *> q1, q2;
        if (!root) return res;
        q1.push(root);
        while (!q1.empty()) {
            vector<int> vals;
            do {
                TreeNode *n = q1.front();
                vals.push_back(n->val);
                if (n->left) q2.push(n->left);
                if (n->right) q2.push(n->right);
                q1.pop();
            } while (!q1.empty());
            // push back current set of results
            res.push_back(vals);
            // swap q1 and q2
            q1.swap(q2);
        }
        return res;
    }
};
